package file;

import org.junit.jupiter.api.Test;

import java.io.*;

public class BinaryFileCopyByBuffered {

    @Test
    public void test() throws IOException {
        //文件源地址和目标地址
        String srcFilePath = "D:\\aaaa\\testCopy.jpg";
        String desFilePath = "D:\\aaaa\\testCopy3.jpg";
        //创建InputStream和OutputStream流
        FileInputStream fileInputStream = new FileInputStream(srcFilePath);
        FileOutputStream fileOutputStream = new FileOutputStream(desFilePath);

        //包装上面的流
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        //读取并且写入


/*
//单字节读取并写入
        int readByte;
        while((readByte=bufferedInputStream.read())!=-1){
            bufferedOutputStream.write(readByte);
        }
*/
        //一次读取多个字节并写入
        byte[] bytes = new byte[1024];
        int readLength = 0;
        while((readLength=bufferedInputStream.read(bytes))!=-1){
            bufferedOutputStream.write(bytes);
        }


        //关闭流
        bufferedInputStream.close();
        bufferedOutputStream.close();




    }
}
